package cn.cdut.yiwenqian.dao.impl;

import java.util.Date;
import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.cdut.yiwenqian.dao.interfaces.IToolDao;
import cn.cdut.yiwenqian.entity.CdutPostTools;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Scope("singleton")
@Repository
public class ToolDaoImpl extends HibernateDaoSupport implements IToolDao {

	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

	@Override
	public CdutPostTools findToolById(Integer id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(CdutPostTools.class, id);
	}

	@Override
	public MyPageToolVo<CdutPostTools> findToolByAhther(final Integer auther, final int page) {
		MyPageToolVo<CdutPostTools> rpage=null;
		rpage=this.getHibernateTemplate().execute(new HibernateCallback<MyPageToolVo<CdutPostTools>>(){

			@Override
			public MyPageToolVo<CdutPostTools> doInHibernate(Session session) throws HibernateException {
				MyPageToolVo<CdutPostTools> repage =new MyPageToolVo<CdutPostTools>();
				StringBuffer sql=new StringBuffer();
				sql.append("from CdutPostTools t where t.cdutPostUsers.unumber = :auther");
				Query query=session.createQuery(sql.toString());
				query.setInteger("auther", auther);
				repage.setPageCount(query.list().size());
				query.setFirstResult((page-1)*15);
				query.setMaxResults(15);
				repage.setPageNum(page);
				repage.setList(query.list());
				return repage;
			}
			
		});
		return rpage;
	}

	@Override
	public MyPageToolVo<CdutPostTools> fingToolByName(final String name, final int page) {
		MyPageToolVo<CdutPostTools> rpage=null;
		rpage=this.getHibernateTemplate().execute(new HibernateCallback<MyPageToolVo<CdutPostTools>>(){

			@Override
			public MyPageToolVo<CdutPostTools> doInHibernate(Session session) throws HibernateException {
				MyPageToolVo<CdutPostTools> repage =new MyPageToolVo<CdutPostTools>();
				StringBuffer sql=new StringBuffer();
				sql.append("from CdutPostTools t where t.name like ?");
				Query query=session.createQuery(sql.toString());
				query.setString(0, name);
				repage.setPageCount(query.list().size());
				query.setFirstResult((page-1)*15);
				query.setMaxResults(15);
				repage.setPageNum(page);
				repage.setList(query.list());
				return repage;
			}
			
		});
		return rpage;
	}

	@Override
	public CdutPostTools saveTool(CdutPostTools tool) {
		tool.setBeginDate(new Date());
		this.getHibernateTemplate().save(tool);
		return tool;
	}

	@Override
	public void updateTool(CdutPostTools tool) {
		this.getHibernateTemplate().update(tool);
		
	}
}
